package com.cl.park.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.cl.park.dao.BaseDao;
import com.cl.park.model.Car;
import com.cl.park.model.Log;
import com.cl.park.model.Park;
import com.cl.park.model.Rule;
import com.cl.park.model.Vip;
import com.cl.park.service.VpService;

public class VpServiceImpl implements VpService{
	
	public BaseDao baseDao;
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
	public String addMoneyVip(Vip vip, Double money) {
		Vip now = (Vip)this.baseDao.getUniqueByHql("from VIP where phone = '"+vip.getPhone()+"'");
		Double moneyNew = now.getMoney()+money;
		now.setMoney(moneyNew);
		this.baseDao.update(now);
		return "ok";
	}

	public String addVIP(Vip vip) {
		Vip p = (Vip)this.baseDao.getByNProperty(Vip.class, "phone",vip.getPhone());
		if(p!=null){
			return "phone";
		}
		vip.setState(1);
		SimpleDateFormat sdf = new SimpleDateFormat();
		vip.setLasttime(sdf.format(new Date()));
		this.baseDao.add(vip);
		return "ok";
	}

	public String leave(Park park, Integer ent) {
		park = (Park)this.baseDao.getByNProperty(Park.class,"name", park.getName());
		Car car = (Car)this.baseDao.get(Car.class, park.getCar());
		if(car.getVipNo()==null){
			//非会员收费标准
			Rule rule = (Rule)this.baseDao.get(Rule.class, 1);
			Date time = park.getTime();
			Long bet = new Date().getTime() - time.getTime();
			Long h = bet / 1000 / 60 /60;
			double hour = h.intValue();
			hour = myround(hour);
			if(hour==0.0){
				hour = 1.0;
			}
			double money = rule.getMoney() * myround(hour); 
			
			Log log = new Log();
			log.setCarNo(car.getCarNo());
			log.setDescription(new Date()+"-从"+ent+"号出口离开停车场-费用："+money+"元");
			this.baseDao.add(log);
			park.setCar(null);
			park.setName("空");
			park.setState(1);
			this.baseDao.update(park);
			return "停车费用："+money;
		}else{
			//会员收费标准
			Vip vip = (Vip)this.baseDao.get(Vip.class, car.getVipNo());
			Rule rule = (Rule)this.baseDao.get(Rule.class, 2);
			Date time = park.getTime();
			Long bet = new Date().getTime() - time.getTime();
			Long h = bet / 1000 / 60 /60;
			double hour = h.intValue();
			hour = myround(hour);
			if(hour==0.0){
				hour = 1.0;
			}
			double money = rule.getMoney() * hour; 
			double y = vip.getMoney() - money;
			if(y<0){
				return "no";
			}else{
				vip.setMoney(y);
				this.baseDao.update(vip);
				Log log = new Log();
				log.setCarNo(car.getCarNo());
				log.setDescription(new Date()+"-从"+ent+"号出口离开停车场-费用："+money+"元");
				this.baseDao.add(log);
				park.setCar(0);
				park.setName("空");
				park.setState(0);
				this.baseDao.update(park);
				return "本次费用："+money+"--余额："+y;
			}
			
		}
	}
	
	public List<Object> lookLog(String carNo) {
		String hql = "from com.cl.park.model.Log where 1=1 ";
		if(carNo!=null && !carNo.equals("")){
			hql+=" and carNo = '"+carNo+"'";
		}
		List<Object> list = this.baseDao.getListByHql(hql);
		return list;
	}

	public String park(Park park, Integer ent,String carNo) {
		Park park1 = (Park)this.baseDao.get(Park.class, park.getId());
		Car car = (Car)this.baseDao.getByNProperty(Car.class, "carNo",carNo);
		if(car==null){
			return "none";
		}
		park1.setCar(car.getId());
		park1.setName(car.getCarNo());
		park1.setState(1);
		park1.setTime(new Date());
		this.baseDao.update(park1);
		Log log = new Log();
		log.setCarNo(car.getCarNo());
		log.setDescription(new Date()+"-由"+ent+"号出入口进入停车场");
		this.baseDao.add(log);
		return "ok";
	}

	public String updateVip(Vip vip) {
		Vip vip1 = (Vip)this.baseDao.get(Vip.class, vip.getId());
		vip1.setPhone(vip.getPhone());
		vip1.setRealName(vip.getRealName());
		vip1.setState(vip.getState());
		this.baseDao.update(vip1);
		return "ok";
	}
	
	public double myround(double num)
	 {
	   BigDecimal b=new BigDecimal(num);
	   num=b.setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();
	   return num;
	 }

	public List<Object> listPark(Park park) {
		String hql = "from Park where 1=1";
		if(park.getFloor()!=null){
			hql+=" and floor = "+park.getFloor();
		}
		if(park.getState()!=null){
			hql+=" and state = "+park.getState();
		}
		List<Object> list = this.baseDao.getListByHql(hql);
		return list;
	}

	public List<Object> listVip(Vip vip) {
		String hql ="from Vip where 1=1";
		if(null != vip){
			if(vip.getPhone()!=null&&!vip.getPhone().equals("")){
				hql+=" and phone like '%"+vip.getPhone()+"%'";
			}
			if(vip.getRealName()!=null&&!vip.equals("")){
				hql+=" and realName like '%"+vip.getRealName()+"%'";
			}
		}
		List<Object> list = this.baseDao.getListByHql(hql);
		return list;
	}

	public Object getOne(String classes, Integer id) {
		String hql = "from "+classes+" where id ="+id;
		Object obj =this.baseDao.getUniqueByHql(hql);
		return obj;
	}

	public Integer addCar(Car car) {
		Car car1 = (Car)this.baseDao.getByNProperty(Car.class, "carNo",car.getCarNo());
		if(car1!=null){
			return car1.getId();
		}
		this.baseDao.add(car);
		return car.getId();
	}

}
